<?php

namespace Admin\Models\coop;

use Admin\Models\BaseModel;
use Helpers\Cache;

class XhCoopInfo extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iCIId;

    /**
     *
     * @var string
     */
    public $sName;

    /**
     *
     * @var int
     */
    public $iType;

    /**
     *
     * @var string
     */
    public $sLegalName;

    /**
     *
     * @var string
     */
    public $sPhoneNum;

    /**
     *
     * @var string
     */
    public $sAddress;

    /**
     * @var string
     */
    public $mDesc;

    /**
     * @var int
     */
    public $iDepId;


    /**
     * @var string
     */
    public $sProdIds;

    /**
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHCoop_Info';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhCoopInfo[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhCoopInfo
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }


    /**
     * 获取供应商
     *
     * @return array|bool|mixed|null
     *
     * create by dale  20180719
     */
    public static function getSupply(){
        if (Cache::exists('Coop_Supply')) {
            return Cache::get('Coop_Supply');
        }
        $supply = self::find([
            'columns'    => 'iCIId,sName',
            'conditions' => 'bDelete = 0 and iType = 2',
        ])->toArray();
        $supply = array_column($supply, 'sName', 'iCIId');
        Cache::save('Coop_Supply', $supply);
        return $supply;
    }

    /**
     * 获取代理商
     *
     * @return array|bool|mixed|null
     *
     * create by dale  20180719
     */
    public static function getAgency(){
        if (Cache::exists('Coop_Agency')) {
            return Cache::get('Coop_Agency');
        }
        $obj = self::find([
            'columns'    => 'iCIId,sName',
            'conditions' => 'bDelete = 0 and iType = 1',
        ])->toArray();
        $obj = array_column($obj, 'sName', 'iCIId');
        Cache::save('Coop_Agency', $obj);
        return $obj;
    }


    public function beforeUpdate()
    {
        if (Cache::exists('Coop_Agency')) {//代理
            Cache::delete('Coop_Agency');
        }
        if (Cache::exists('Coop_Supply')) {//供应
            Cache::delete('Coop_Supply');
        }
        return parent::beforeUpdate(); // TODO: Change the autogenerated stub
    }

    public function beforeCreate()
    {
        if (Cache::exists('Coop_Agency')) {
            Cache::delete('Coop_Agency');
        }
        if (Cache::exists('Coop_Supply')) {
            Cache::delete('Coop_Supply');
        }
        return parent::beforeCreate(); // TODO: Change the autogenerated stub
    }

}
